package ec.edu.udla.fichamedica.client;

import java.io.IOException;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.text.shared.Renderer;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ValueListBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;

import ec.edu.udla.fichamedica.client.ui.PersonList;
import ec.edu.udla.fichamedica.domain.DocumentType;
import ec.edu.udla.fichamedica.domain.GenderType;
import ec.edu.udla.fichamedica.domain.Person;

public class Home extends Composite implements Editor<Person> {

	private static HomeUiBinder uiBinder = GWT.create(HomeUiBinder.class);

	interface HomeUiBinder extends UiBinder<Widget, Home> {
	}

	public Home() {
		initWidget(uiBinder.createAndBindUi(this));
		initEnums();
		
	}

	private void initEnums() {
		
			documentType.setAcceptableValues(EnumUtil.getDocumentTypes());
			genderType.setAcceptableValues(EnumUtil.getGenderTypes());
		
	}

	@UiField
	Image image;

	@UiField
	TextBox name;

	@UiField
	TextBox surname;
	@UiField
	Button btnGuardar;
	@UiField
	DateBox birthdate;
	@UiField TextBox codigo;
	@UiField TextBox pin;
	@UiField Button cancelar;
	@UiField Button eliminar;
	@UiField(provided = true) ValueListBox<DocumentType> documentType = 
		new ValueListBox<DocumentType>(new Renderer<DocumentType>() {

			@Override
			public String render(DocumentType docType) {
				if(docType==null)
					return "-Seleccione-";
					return docType.name();
				
			}

			@Override
			public void render(DocumentType object, Appendable appendable)
					throws IOException {
				render(object);
				
			}
		});
	
		
		@UiField(provided = true) ValueListBox<GenderType> genderType=new ValueListBox<GenderType>(new Renderer<GenderType>() {

			@Override
			public String render(GenderType genderType) {
				if(genderType==null)
					return "-Seleccione-";
					return genderType.name();
				
			}

			@Override
			public void render(GenderType object, Appendable appendable)
					throws IOException {
				render(object);
				
			}
		});
	List<Person> persons;

	@UiHandler("btnGuardar")
	void onClick(ClickEvent e) {

		image.setUrl("http://www.google.com/intl/en_ALL/images/logo.gif");
		Person p = FichaMedicaWebApp.driver.flush();
		
		if(p.getId()==null){
			//TODO: binding de listBox
//			p.setGenderType(GenderType.valueOf(genderType.getItemText(genderType.getSelectedIndex())));
//			p.setDocumentType(DocumentType.valueOf(documentType.getItemText(documentType.getSelectedIndex())));
			EntityServiceAsync entityService = GWT.create(EntityService.class);

			entityService.createPerson(p, new AsyncCallback<Boolean>() {

				@Override
				public void onFailure(Throwable error) {
					System.out.println("Error ..... : "
							+ error.getLocalizedMessage());
					error.printStackTrace();
				}

				@Override
				public void onSuccess(Boolean result) {
					System.out.println("Todo ok .... ");
					regresar();
				}
			});
		}else{
			EntityServiceAsync entityService = GWT.create(EntityService.class);
//			p.setGenderType(GenderType.valueOf(genderType.getItemText(genderType.getSelectedIndex())));
//			p.setDocumentType(DocumentType.valueOf(documentType.getItemText(documentType.getSelectedIndex())));
			entityService.updatePerson(p, new AsyncCallback<Boolean>() {

				@Override
				public void onFailure(Throwable error) {
					System.out.println("Error ..... : "
							+ error.getLocalizedMessage());
					error.printStackTrace();
				}

				@Override
				public void onSuccess(Boolean result) {
					System.out.println("Todo ok .... ");
					regresar();
				}
			});
		}

	}
	
	public void regresar(){
		RootPanel.get().clear();

		PersonList list = new PersonList();
		RootPanel.get().add(list);
	}

	@UiHandler("cancelar")
	void onCancelarClick(ClickEvent event) {
		regresar();
	}
	@UiHandler("eliminar")
	void onEliminarClick(ClickEvent event) {
		
		Person p = FichaMedicaWebApp.driver.flush();
		
		EntityServiceAsync entityService = GWT.create(EntityService.class);

		entityService.deletePerson(p, new AsyncCallback<Boolean>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Boolean result) {
				// TODO Auto-generated method stub
				regresar();
			}
		});
		
	}
}
